package com.example.administrator.caigou51.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.EventBusClass.UpdateShopCarFragment;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.bean.UserCenterOtherBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.UserCenterCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import de.greenrobot.event.EventBus;


/**
 * Created by Administrator on 2016/5/19/019.
 */
public class UserCenterFragment extends BaseFragment{
    private View holderView;
    private MaterialListView material_listview;
    RelativeLayout mTopbar;
    private boolean isScaled = false;
    UserCenterCard userCenterCard;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (holderView == null) {
            holderView = inflater.inflate(R.layout.template_materiallistview_layout, container, false);
            material_listview= (MaterialListView) holderView.findViewById(R.id.material_listview);
            mTopbar = (RelativeLayout) holderView.findViewById(R.id.topbar);
            EventBus.getDefault().register(this);
            if (App.getUserBean()!=null) {
                TaskUpdatOrderCount((GBaseActivity) getActivity());
            }
        }
        ViewGroup parent = (ViewGroup) holderView.getParent();
        if (parent != null) {
            parent.removeView(holderView);
        }
        if (!isScaled) {
            AbViewUtil.scaleContentView((ViewGroup) holderView);
            isScaled = true;
        }

        return holderView;
    }





    protected void fillArrayInListView(UserCenterOtherBean bean) {
        if (userCenterCard==null) {
            userCenterCard = new UserCenterCard(getActivity());
            userCenterCard.setUserCenterOtherBean(bean);
            material_listview.add(userCenterCard);
        }else
        {
            userCenterCard.setUserCenterOtherBean(bean);
            material_listview.getAdapter().notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(UpdateShopCarFragment updateShopCarFragment) {
        material_listview.getAdapter().notifyDataSetChanged();
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            TaskUpdatOrderCount((GBaseActivity) getActivity());
            //相当于Fragment的onResume
        } else {
            //相当于Fragment的onPause
        }
    }

    private void TaskUpdatOrderCount( GBaseActivity gBaseActivity) {
        RequestServerManager.getInstanceDIY().handleMethodDIY(gBaseActivity, material_listview, false, null,
                Constant.getRootUrl() + Constant.Action.Action_UserCenterOther, Constant.Action.Action_UserCenterOther, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_UserCenterOther,App.getUserBean().getUserid(),App.getUserBean().getSession_id()), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        UserCenterOtherBean userCenterOtherBean = JSON.parseObject(result.getData().toString(),UserCenterOtherBean.class);
                        App.saveUserCenterOtherBean(userCenterOtherBean);
                        fillArrayInListView(userCenterOtherBean);
                       //  Log.e(" result.getData()", result.getData().toString());
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }

}
